package com.bolingcavalry.crm.contacts.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.base.PageResult;
import com.base.ResponseResult;
import com.bolingcavalry.crm.contacts.service.ContactsService;
import com.dto.CustomerContactDTO;
import com.vo.AfterSales.AfterSalesVO;
import com.vo.CustomerContactVO;
import com.vo.ServiceProblemVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/contacts")
public class ContactsController {
    @Autowired
    private ContactsService contactsService;

    //新增用户
    @PostMapping("/add")
    public ResponseResult add(@RequestBody CustomerContactDTO customerContactDTO){
        if(contactsService.add(customerContactDTO)){
            return ResponseResult.success("新增用户成功");
        }
        return ResponseResult.error("新增用户失败，该用户已存在");
    }

    //删除用户
    @DeleteMapping("/delete")
    public ResponseResult delete(@RequestBody List<Long> ids){
        if(contactsService.batchDelete(ids)){
            return ResponseResult.success("删除用户成功");
        }
        return ResponseResult.error("删除用户失败请检查是否包含在订单中");
    }

    //更新用户
    @PostMapping("/update")
    public ResponseResult update(@RequestBody CustomerContactDTO customerContactDTO){
        if(contactsService.update(customerContactDTO)){
            return ResponseResult.success("更新用户成功");
        }
        return ResponseResult.error("更新用户失败，请检查是否重复");
    }

    //get查询用户
    @GetMapping("/get/{id}")
    public ResponseResult<CustomerContactVO> get(@PathVariable Long id){
        CustomerContactVO customerContactVO = contactsService.get(id);
        if(null != customerContactVO){
            return ResponseResult.success(customerContactVO);
        }
        return ResponseResult.error("未找到该用户");
    }

    //根据客户公司id查询联系人列表
    @GetMapping("/listByCustomerCompanyId/{id}")
    public ResponseResult<List<CustomerContactVO>> listByCustomerCompanyId(@PathVariable Long id){
        List<CustomerContactVO> list = contactsService.listByCustomerCompanyId(id);
        return ResponseResult.success("查询成功",list);
    }

    //分页条件查询用户列表
    @GetMapping("/list")
    public ResponseResult<IPage<CustomerContactVO>> list(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(required = false) Integer pageSize,
            @RequestParam(required = false) String customerCompanyName,
            @RequestParam(required = false) String fullName,
            @RequestParam(required = false) String phoneNumber){
        IPage<CustomerContactVO> page = contactsService.list(pageNum,pageSize,customerCompanyName, fullName, phoneNumber);
        PageResult<CustomerContactVO> result = new PageResult<>();
        result.setTotal(page.getTotal());
        result.setPages((int) page.getPages());
        result.setCurrent((int) page.getCurrent());
        result.setSize((int) page.getSize());
        result.setRecords(page.getRecords());
        return ResponseResult.success("查询成功",result);
    }

    //根据客户id查询对应的联系人列表
    @GetMapping("/listByCustomerId/{customerId}")
    public ResponseResult<List<CustomerContactVO>> listByCustomerId(@PathVariable Long customerId){
        //查询联系人列表
        List<CustomerContactVO> list = contactsService.listByCustomerId(customerId);
        return ResponseResult.success("查询成功",list);
    }
    //设置主要联系人为1，其他的自动为0？
    //传入用户id
    @PostMapping("/setPrimary")
    public ResponseResult setPrimary(@RequestParam Long id){
        if(contactsService.setPrimary(id)){
            return ResponseResult.success("设置主要联系人成功");
        }
        return ResponseResult.error("设置主要联系人失败");
    }

    //根据客户id查询对应的售后列表
    @GetMapping("/listAfterSalesByCustomerId/{id}")
    public ResponseResult<List<AfterSalesVO>> listAfterSalesByCustomerId(@PathVariable Long id){
        //查询联系人列表
        List<AfterSalesVO> list = contactsService.listByCustomerCompanyIdAfterSales(id);
        return ResponseResult.success("查询成功",list);
    }

    //根据客户id查询对应的服务问题列表
    @GetMapping("/listServiceProblemByCustomerId/{id}")
    public ResponseResult<List<ServiceProblemVO>> listServiceProblemByCustomerId(@PathVariable Long id){
        //查询联系人列表
        List<ServiceProblemVO> list = contactsService.listByCustomerCompanyIdServiceProblem(id);
        return ResponseResult.success("查询成功",list);
    }




}
